Expressions with arithmetic (/, *, %, ++, --, -, -=, *=, /=, %=, +=, +
), unary (-
), or comparison operators (>,
<, >=, <=
) where one, or both, of the operands is a String, Boolean or Date value rely on implicit conversions. Both the
maintainability and reliability levels of such a piece of code are questionable.
Noncompliant code example
str = "80";
quarter = str / 4; // Noncompliant
if (str < 10) { // Noncompliant
// ...
}
Compliant solution
str = "80";
parsedStr = parseInt(str);
quarter = parsedStr / 4;
if (parsedStr < 10) {
// ...
}
Exceptions
- Expressions using the binary
+
operator with at least one String
operand are ignored because the +
operator will perform a concatenation in that case.
- Comparisons where both operands are strings are ignored because a lexicographical comparison is performed in that case.